package com.generator.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.Enums.ResponseEnum;
import com.common.exception.CourseException;
import com.common.util.CopyUtil;
import com.generator.dto.ValueRuleDto;
import com.generator.entity.ValueRule;
import com.generator.mapper.ValueRuleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ValueRuleServiceImp extends ServiceImpl<ValueRuleMapper, ValueRule> implements ValueRuleService{

   @Autowired
   private ValueRuleMapper  valueRuleMapper;
  
   @Override
    public ResponseEntity list(Page pageDto,  ValueRule valueRule) {
        IPage<ValueRule> page = valueRuleMapper.selectPage(new Page<>(pageDto.getCurrent(), pageDto.getSize()),
                new LambdaQueryWrapper<ValueRule>(valueRule));
        pageDto.setTotal(page.getTotal());
        List<ValueRuleDto> valueRuleDtoList = CopyUtil.copyList(page.getRecords(), ValueRuleDto.class);
        pageDto.setRecords(valueRuleDtoList);
        return ResponseEntity.ok(pageDto);
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @Override
    public ResponseEntity save( ValueRuleDto valueRuleDto) {
        ValueRule valueRule = CopyUtil.copy(valueRuleDto, ValueRule.class);
        if (StringUtils.isEmpty(valueRuleDto.getId())) {
            return this.insert(valueRule);
        } else {
            return this.update(valueRule);
        }

    }

    /**
     * 更新
     */
    private ResponseEntity update(ValueRule valueRule) {
        int i = valueRuleMapper.updateById(valueRule);
        if (i <= 0) {
            throw new CourseException(ResponseEnum.UPDATE_ERROR);
        }
        return ResponseEntity.ok(ResponseEnum.UPD_SUCCESS.getDesc());
    }

    /**
     * 新增
     */
    private ResponseEntity insert(ValueRule valueRule) {
        int i = valueRuleMapper.insert(valueRule);
        if (i <= 0) {
            throw new CourseException(ResponseEnum.ADD_ERROR);
        }
        return ResponseEntity.ok(ResponseEnum.ADD_SUCCESS.getDesc());
    }


    /**
     * 删除
     */
    @Override
    public ResponseEntity delAll(List<Integer> ids) {
        int i = valueRuleMapper.deleteBatchIds(ids);
        if (i <= 0) {
            throw new CourseException(ResponseEnum.DELETE_ERROR);
        }
        return ResponseEntity.ok(ResponseEnum.DEL_SUCCESS.getDesc());
    }

    /**
     * 根据数据库配置和列名获取值规则
     *
     * @param id
     * @param column
     */
    @Override
    public ResponseEntity getcolumnvalueRule(String id, String column) {
        ValueRule valueRule = baseMapper.selectOne(new LambdaQueryWrapper<ValueRule>().eq(ValueRule::getDatabsourceId, id).eq(ValueRule::getColumn, column));
        return ResponseEntity.ok(valueRule);
    }
}